package com.feng.dao.user;

import com.feng.dao.BaseDao;
import com.feng.pojo.User;
import org.junit.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao {

    public User queryUser(Connection conn, String username,String password) {

        Connection connection = conn;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        String sql = "select * from subsystem.user where username=? and password=?";
        User user = null;

        try {
            preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1, username);
            preparedStatement.setString(2, password);


            resultSet = preparedStatement.executeQuery();

            if (resultSet.next()) {
                user = new User();
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                user.setRealname(resultSet.getString("realname"));
                user.setRole(resultSet.getString("role"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        BaseDao.CloseResources(null, preparedStatement, resultSet);

        return user;
    }

    @Test
    public void test() {
        User user = queryUser(BaseDao.getConnection(), "qq1234567","1111111");
        System.out.println(user);

    }
}
